capture log close

// Set the directory to your own. Here is an example
cd "~/Dropbox/2018 Coalitions/Documentation/Replication files/"

// Start log. Here is an example
log using aq-analyze-table-dv-quota, replace text

// program: Stata 12.1
// task:  Make Tables 3 and 4 and run robustness checks
// project: Kang and Tripp 2018 Perspectives on Politics
// author: Alice J. Kang 

version 12.1
clear all
set linesize 80
set more off
* To make the tables, which we then edited in Word, we use outreg.
ssc install outreg

// Bring in data
use KangTrippReplication.dta

// Attention: The data set does not include iwm_cum2 and
// interactions with those variables.

// To obtain iwm_cum2 and make iwm_cum2, please contact the authors of 
// Hughes et al (2015, International Studies Quarterly).

// Here is the code we used to make iwm_cum2 and the interaction term:

/* 
sort year iwm_cum
gen iwm_temp=iwm_cum
by year: replace iwm_temp=iwm_temp[_n-1] if iwm_temp==.
sort country year
by country: ipolate iwm_temp year if year>=1989, gen(iwm_cum2) epolate 
label variable iwm_cum2 "Global pressure, extrapolated (based on Hughes et al)" 

gen iwm_cum2xbpc=iwm_cum2*lbeijingpc
label variable iwm_cum2xbpc "Interaction: iwm_cum2 x lbeijingpc" 
*/

// #1: Make entry variable due to different independence years
*Late entry: Eritrea in 1993, Namibia in 1990
sort country year
gen enter=1989
by country: replace enter=1993 if country=="Eritrea"
by country: replace enter=1990 if country=="Namibia"
label variable enter "1989 or year of independence if after"

// #2: Make begin variable
* Begin risk at 1989
gen begin=1989

// #3: Set
stset year, id(country) failure(quota==1) enter (time enter-1) ///
	origin(time begin-1)

// #4: Make time variables
rename _t t
gen pluralt=plural_lead*t

// #5: Make Table 3
* Model 1, Complementary log-log: International
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle t if groups2!=. & plural_lead!=., ///
	cluster(country)
outreg using Table3.doc, se starlevels(10 5 1 .1) sigsymbols(+,*,**,***) ///
	ctitles("", 1) bdec(2) summstat(ll\r2_p\N) ///
	summtitle(Log likelihood\Pseudo R-squared\N) replace
estat ic
 
* Model 2, Complementary log-log: Add domestic. Add interaction with plurality to address
* violation of the proportionality assumption
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl musmaj t pluralt, cluster(country)
outreg using Table3.doc, se starlevels(10 5 1 .1) sigsymbols(+,*,**,***) ///
	ctitles("", 2) bdec(2) summstat(ll\r2_p\N) ///
	summtitle(Log likelihood\Pseudo R-squared\N) merge replace
estat ic

* Model 3, Complementary log-log: Add coalition. Add interaction with plurality to address
* violation of the proportionality assumption
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition t pluralt, cluster(country)
outreg using Table3.doc, se starlevels(10 5 1 .1) sigsymbols(+,*,**,***) ///
	ctitles("", 3) bdec(2) summstat(ll\r2_p\N) ///
	summtitle(Log likelihood\Pseudo R-squared\N) merge replace
estat ic

* Model 4, Complementary log-log: Full sample of countries
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 ///
	l2gnipcatlasl musmaj coalition t, cluster(country)
outreg using Table3.doc, se starlevels(10 5 1 .1) sigsymbols(+,*,**,***) ///
	ctitles("", 4) bdec(2) summstat(ll\r2_p\N) ///
	summtitle(Log likelihood\Pseudo R-squared\N) merge replace
estat ic

// #6: Predict
* go to quotapredict.do file

// #7: Make Table 4
* Complementary log-log: Democratic transition
cloglog _d coalition iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 ///
	groups2 demtransp l2gnipcatlasl musmaj t if country!="Somalia", cluster(country)
outreg using Table4test.doc, se starlevels(10 5 1 .1) sigsymbols(+,*,**,***) ///
	ctitles("", 1) bdec(2) summstat(ll\r2_p\N) ///
	summtitle(Log likelihood\Pseudo R-squared\N) replace
	
* Complementary log-log: Left leaning rule
cloglog _d coalition iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 ///
	groups2 left2 l2gnipcatlasl musmaj t if country!="Somalia", cluster(country)
outreg using Table4test.doc, se starlevels(10 5 1 .1) sigsymbols(+,*,**,***) ///
	ctitles("", 2) bdec(2) summstat(ll\r2_p\N) ///
	summtitle(Log likelihood\Pseudo R-squared\N) merge replace
	
* Complementary log-log: Women in parliament
cloglog _d coalition iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 ///
	groups2 wparl3 l2gnipcatlasl musmaj t if country!="Somalia", cluster(country)
outreg using Table4test.doc, se starlevels(10 5 1 .1) sigsymbols(+,*,**,***) ///
	ctitles("", 3) bdec(2) summstat(ll\r2_p\N) ///
	summtitle(Log likelihood\Pseudo R-squared\N) merge replace	

* Complementary log-log: Women's labor force
cloglog _d coalition iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 ///
	groups2 labor l2gnipcatlasl musmaj t if country!="Somalia", cluster(country)
outreg using Table4test.doc, se starlevels(10 5 1 .1) sigsymbols(+,*,**,***) ///
	ctitles("", 4) bdec(2) summstat(ll\r2_p\N) ///
	summtitle(Log likelihood\Pseudo R-squared\N) merge replace
estat ic
 
* Complementary log-log: -2008
cloglog _d coalition iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 ///
	groups2 l2gnipcatlasl musmaj t if country!="Somalia" & year<=2008, cluster(country)
outreg using Table4test.doc, se starlevels(10 5 1 .1) sigsymbols(+,*,**,***) ///
	ctitles("", 5) bdec(2) summstat(ll\r2_p\N) ///
	summtitle(Log likelihood\Pseudo R-squared\N) merge replace

// #8: Robustness check to Table 3, Model 3

* Don't model for time dependence, no interaction with time
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition, cluster(country) 

* Include cubic time, no interaction with time
gen t2=_t*_t
gen t3=_t*_t*_t
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition t t2 t3, cluster(country)

* PREG instead of Ethnic Power Relations
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 preg90 plural_lead ///
	l2gnipcatlasl musmaj coalition t pluralt, cluster(country)
	
* WINGO instead of Beijing based variable. Use Hughes et al, which goes through 2008 only.
* Please contact Hughes et al for the variable, pwingo
cloglog _d iwm_cum pwingo pwingoxxiwm ldacpcle conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition t pluralt, cluster(country) 

* UNPKO in addition to our postconflict variable
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle unpko conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition t pluralt, cluster(country)
	
* Women's rights accords in addition to our postconflict variable: drops
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle gendaccord2 conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition t pluralt, cluster(country)
 
* Official state religion instead of Muslim majority. Note there is not much variation here
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl staterel coalition t pluralt, cluster(country)
	
* Percent Catholic instead of Muslim majority
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl dccathol coalition t pluralt, cluster(country)
	
* How early a country ratified CEDAW. Missing if not ratified.
cloglog _d cedaw_year iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition t pluralt, cluster(country)

* Omit plurality variable
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 groups2 ///
	l2gnipcatlasl musmaj coalition t pluralt, cluster(country)

* Logit
logit _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 plural_lead groups2 ///
	l2gnipcatlasl musmaj coalition t pluralt, cluster(country)
	
* Cox: with and without interaction with time
* Change t back to _t
rename t _t
stcox iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle polity2 conf_cat23 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition, cluster(country)
	
stcox iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle polity2 conf_cat23 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition pluralt, cluster(country)
	
* Weibull
streg iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle polity2 conf_cat23 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition, cluster(country) d(weib)
	
* Exclude potentially influential countries
* To identify potential outliers, use stcox and predict martingale and deviance residuals
stcox iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition pluralt, cluster(country)
predict mg, mgale
predict xb, xb
scatter mg xb, mlabel(country)
predict dev, deviance
scatter dev xb, mlabel(country)
* Tanzania could be an outlier
stcox iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition pluralt if country!="Tanzania", cluster(country)

// #9: Robustness check to Table 4: No t
* Complementary log-log: Democratic transition
cloglog _d coalition iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 ///
	demtransp l2gnipcatlasl musmaj, cluster(country)
	
* Complementary log-log: Left leaning rule
cloglog _d coalition iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 left2 ///
	l2gnipcatlasl musmaj, cluster(country)

* Complementary log-log: Women in parliament
cloglog _d coalition iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 wparl3 ///
	l2gnipcatlasl musmaj, cluster(country)

* Complementary log-log: Women's labor force
cloglog _d coalition iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 ///
	labor l2gnipcatlasl musmaj, cluster(country)
 
* Complementary log-log: -2008
cloglog _d coalition iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 ///
	l2gnipcatlasl musmaj if year<=2008, cluster(country)
	
// #10: Recode Liberia to have no quota
clear all
use KangTrippReplication.dta
sort country year
gen enter=1989
by country: replace enter=1993 if country=="Eritrea"
by country: replace enter=1990 if country=="Namibia"
label variable enter "1989 or year of independence if after"
replace quota=0 if country=="Liberia"
gen begin=1989
stset year, id(country) failure(quota==1) enter (time enter-1) ///
	origin(time begin-1)
rename _t t
gen pluralt=plural_lead*t
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition t pluralt, cluster(country)
clear
	
log close
exit
